/*
 DNN Content Builder - sourceforge.net/projects/dnncb
 Copyright (c) 2004-2008
 by Emerson Lopes (emersonlopes@gmail.com)

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.
*/

namespace DNNContentBuilder.UserControls
{
	using System;
	using System.Collections;
	using System.IO;
	using System.Text;

	/// <summary>
	///		Summary description.
	/// </summary>
	public class TemplateEditControl : DotNetNuke.Entities.Modules.PortalModuleBase
	{
		public const string resource = "~/DesktopModules/DNNContentBuilder/App_LocalResources/TemplateEditControl";

		protected System.Web.UI.WebControls.LinkButton btUpdate;
		protected System.Web.UI.WebControls.ListBox lbLabels;
		protected System.Web.UI.WebControls.DropDownList ddForms;
		protected System.Web.UI.WebControls.LinkButton btCancel;

		protected LabelController labelController = new LabelController();
		protected FormDefinitionController fdController = new FormDefinitionController();
		protected ConnectionStringController csController = new ConnectionStringController();
		protected TemplateDefinitionController tdController = new TemplateDefinitionController();
		protected TemplateDefinitionInfo tdInfo = new TemplateDefinitionInfo();
		protected ContentViewerSettingsController cvsController = new ContentViewerSettingsController();
		protected System.Web.UI.WebControls.LinkButton btNewLabel;
		protected System.Web.UI.WebControls.LinkButton btCreateLabel;
		protected System.Web.UI.WebControls.LinkButton btCancelLabel;
		protected System.Web.UI.WebControls.TextBox txtLabel;
		protected ContentViewerSettingsInfo cvsInfo = new ContentViewerSettingsInfo();
		protected System.Web.UI.WebControls.ListBox ListBox1;
		protected LabelInfo labelInfo = new LabelInfo();
		protected System.Web.UI.WebControls.Literal ltErrorMessage;

		private void Page_Load(object sender, System.EventArgs e)
		{
			ArrayList cvsList = new ArrayList(cvsController.GetByModule(this.ModuleId));

			if (cvsList.Count == 0) 
			{
				ShowError(Commons.GetLocalizedString("label_ErrorMissingTemplate", resource), "");
				Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(this.TabId));
				return;
			}
			
			this.btCancel.Text			= Commons.GetLocalizedString("button_Cancel", resource);
			this.btCancelLabel.Text		= Commons.GetLocalizedString("button_CancelLabel", resource);
			this.btCreateLabel.Text		= Commons.GetLocalizedString("button_CreateLabel", resource);
			this.btNewLabel.Text		= Commons.GetLocalizedString("button_NewLabel", resource);
			this.btUpdate.Text			= Commons.GetLocalizedString("button_Update", resource);
			
			// Put user code to initialize the page here

			this.ddForms.DataSource = fdController.GetAllFormDefinition();
			this.ddForms.DataTextField = "Title";
			this.ddForms.DataValueField = "FormDefinitionId";
			this.ddForms.DataBind();

			FillLabels();

			if (!IsPostBack) 
			{
				ArrayList list  = new ArrayList(cvsController.GetByModule(this.ModuleId));

				if (list.Count > 0) 
				{
					cvsInfo = (ContentViewerSettingsInfo)list[0];

					tdInfo = tdController.GetItem(cvsInfo.TemplateDefinitionId);

					ddForms.SelectedValue = tdInfo.FormDefinitionId.ToString();
				}
			}
		}

		private void FillLabels()
		{
			this.lbLabels.DataSource = labelController.GetLabelsByModule(this.ModuleId);
			this.lbLabels.DataTextField = "ShortName";
			this.lbLabels.DataValueField = "LabelId";
			this.lbLabels.DataBind();
		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btNewLabel.Click += new System.EventHandler(this.btNewLabel_Click);
			this.btCreateLabel.Click += new System.EventHandler(this.btCreateLabel_Click);
			this.btCancelLabel.Click += new System.EventHandler(this.btCancelLabel_Click);
			this.btUpdate.Click += new System.EventHandler(this.btUpdate_Click);
			this.btCancel.Click += new System.EventHandler(this.btCancel_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}

		#endregion

		private void btUpdate_Click(object sender, System.EventArgs e)
		{
			ArrayList list = new ArrayList(cvsController.GetByModule(this.ModuleId));

			if (list.Count > 0) 
			{
				cvsInfo = (ContentViewerSettingsInfo)list[0];

				tdInfo = tdController.GetItem(cvsInfo.TemplateDefinitionId);
				tdInfo.FormDefinitionId = Int32.Parse(this.ddForms.SelectedValue);

				tdController.Update(tdInfo);
			}
 
			Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(this.TabId));
		}

		private void btCancel_Click(object sender, System.EventArgs e)
		{
			Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());
		}

		private void btNewLabel_Click(object sender, System.EventArgs e)
		{
			this.lbLabels.Visible = false;
			this.btNewLabel.Visible = false;
			
			this.txtLabel.Visible = true;
			this.btCreateLabel.Visible = true;
			this.btCancelLabel.Visible = true;
		}

		private void btCreateLabel_Click(object sender, System.EventArgs e)
		{
			if (txtLabel.Text != "") 
			{
				LabelController labelController = new LabelController();

				if (lbLabels.Items.FindByText(txtLabel.Text) == null) 
				{
					labelInfo.ModuleId = this.ModuleId;
					labelInfo.ShortName = txtLabel.Text;

					labelController.Add(labelInfo);
				}
			}

			ResetLabel();

			FillLabels();
		}

		private void btCancelLabel_Click(object sender, System.EventArgs e)
		{
			ResetLabel();
		}

		private void ResetLabel() 
		{
			this.lbLabels.Visible = true;
			this.btNewLabel.Visible = true;

			this.txtLabel.Visible = false;
			this.btCreateLabel.Visible = false;
			this.btCancelLabel.Visible = false;
		}

		private void ShowError(string message, string error) {
			ltErrorMessage.Visible = true;
			ltErrorMessage.Text = @"
			<script language='javascript'>
			<!-- 
			    alert('{0}') 
			-->
			</script>
			";
			ltErrorMessage.Text = String.Format(ltErrorMessage.Text, message).Replace("\r","").Replace("\n","");
			ltErrorMessage.Text = ltErrorMessage.Text.Replace("<!--","\n<!--\n").Replace("-->","\n-->\n");
		}
	}
}
